package com.example.outdoor;

import android.app.Activity;
import android.app.AlertDialog;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MenuPage extends Activity implements OnClickListener {
	private static String DBNAME = "OutdoorAppDB.db";
	Button importButton;
	Button tableButton;
	Button varificationButton;
	Button exportButton;
	Button logoutButton, btnDeleteAll;
	ProgressBar pbMenuPage;
	SQLiteDatabase db1 = null;
	// shared prefrense to set the login status
	SharedPreferences pref;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setProgressBarIndeterminateVisibility(true);
		setContentView(R.layout.menupage);
		  db1 = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
		pbMenuPage = (ProgressBar) findViewById(R.id.pbMenuPage);
		pref = getSharedPreferences("loginstatus", MODE_PRIVATE);
		importButton = (Button) findViewById(R.id.btnImport);
		tableButton = (Button) findViewById(R.id.btntables);
		varificationButton = (Button) findViewById(R.id.btnverification);
		exportButton = (Button) findViewById(R.id.btnexport);
		logoutButton = (Button) findViewById(R.id.btnlogout);

		importButton.setOnClickListener(this);
		tableButton.setOnClickListener(this);
		varificationButton.setOnClickListener(this);
		exportButton.setOnClickListener(this);
		logoutButton.setOnClickListener(this);
		btnDeleteAll = (Button) findViewById(R.id.btnDeleteAll);
		btnDeleteAll.setOnClickListener(this);
		pbMenuPage.setVisibility(View.GONE);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.btnImport:
			Intent ourIntent = new Intent(MenuPage.this, FileChooser.class);
			startActivity(ourIntent);

			break;
		case R.id.btntables:
			Intent tableIntent = new Intent(MenuPage.this, Tables.class);
			startActivity(tableIntent);
			break;
		case R.id.btnverification:
			Intent varificationIntent = new Intent(MenuPage.this,
					Verification.class);
			startActivity(varificationIntent);
			break;
		case R.id.btnexport:
			Intent exportIntent = new Intent(MenuPage.this, Export.class);
			startActivity(exportIntent);
			break;

		case R.id.btnlogout:
			pref.edit().putString("savedData", "0").commit();
			Intent logoutIntent = new Intent(MenuPage.this, MainActivity.class);
			startActivity(logoutIntent);
			finish();
			break;
		case R.id.btnDeleteAll:
			DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					switch (which) {
					case DialogInterface.BUTTON_POSITIVE:

						db1.execSQL("drop table if exists tblAvenue");
				        MenuPage.this.db1.execSQL("drop table if exists tblBillboard");
				        MenuPage.this.db1.execSQL("drop table if exists tblOBSTRUCTION");
				        MenuPage.this.db1.execSQL("drop table if exists tblCondition");
				        MenuPage.this.db1.execSQL("drop table if exists tblBillboardOutput");
				        MenuPage.this.db1.execSQL("drop table if exists tblANTERIOR");
				        MenuPage.this.db1.execSQL("drop table if exists tblBillboardhistory");
				        MenuPage.this.db1.execSQL("CREATE TABLE IF NOT EXISTS tblBillboard(ID INTEGER PRIMARY KEY , Avenue INTEGER,Date NNVARCHAR,Starttime NVARCHAR,Endtime NVARCHAR,Size NVARCHAR,Carrier NVARCHAR,CarrierDescription NVARCHAR,Quality NVARCHAR,RealCost NVARCHAR,LatandLong NVARCHAR,Numberofads NVARCHAR,Totaladsinthatlocation NVARCHAR,typeofads NVARCHAR,Visibilitycode NVARCHAR,Visibilitydescription NVARCHAR,Verificationdate NVARCHAR,Verificationdescription NVARCHAR,Versiocode NVARCHAR,Versiondescription NVARCHAR,Productcode NVARCHAR,Constant NVARCHAR,CategoryCode NVARCHAR,Categorydescription NVARCHAR,Subsectorcode NVARCHAR,Subsectordescription NVARCHAR,Sectorcode NVARCHAR,Sectordescription NVARCHAR,Betweenavenue NVARCHAR,Brandcode NVARCHAR,Branddescription NVARCHAR,Announcercode NVARCHAR,Announcerdescription NVARCHAR,Imagecode NVARCHAR,Agencycode NVARCHAR,Agencyname NVARCHAR,Condition NVARCHAR,Obstruction NVARCHAR); ");
				        MenuPage.this.db1.execSQL("create table IF NOT EXISTS tblOBSTRUCTION (id INTEGER PRIMARY KEY,Obstruction  TEXT);");
				        MenuPage.this.db1.execSQL("create table IF NOT EXISTS tblCondition (ID INTEGER PRIMARY KEY,Condition  TEXT);");
				        MenuPage.this.db1.execSQL("create table IF NOT EXISTS tblNewProduct (ID INTEGER PRIMARY KEY,Avenue  TEXT,Product VARCHAR,Version VARCHAR);");
				        MenuPage.this.db1.execSQL("CREATE TABLE IF NOT EXISTS tblAvenue(ID INTEGER PRIMARY KEY , Avenue INTEGER,AvenueDescription VARCHAR); ");
				        MenuPage.this.db1.execSQL("CREATE TABLE IF NOT EXISTS tblBillboardOutput(ID INTEGER PRIMARY KEY , Date INTEGER,Time VARCHAR,StartingPoint VARCHAR,Carrier VARCHAR,Size VARCHAR,Product VARCHAR,ProductCode VARCHAR,Version VARCHAR,VersionCode VARCHAR,Condition VARCHAR,Obstraction VARCHAR,NumberofAdd VARCHAR,TotalNumberOfadd VARCHAR,Iluminacion VARCHAR,Saturacion VARCHAR,Latitud VARCHAR,Longitud VARCHAR,ImageCode VARCHAR,Operator VARCHAR,RealCost VARCHAR,Verified VARCHAR,Observacion VARCHAR,avenue VARCHAR); ");
				        MenuPage.this.db1.execSQL("create table IF NOT EXISTS tblANTERIOR (ID INTEGER PRIMARY KEY,Avenue  VARCHAR,BillboardID VARCHAR);");
				        MenuPage.this.db1.execSQL("CREATE TABLE IF NOT EXISTS tblBillboardhistory(ID INTEGER PRIMARY KEY ,OriginalID INTEGER, Avenue INTEGER,Date NNVARCHAR,Starttime NVARCHAR,Endtime NVARCHAR,Size NVARCHAR,Carrier NVARCHAR,CarrierDescription NVARCHAR,Quality NVARCHAR,RealCost NVARCHAR,LatandLong NVARCHAR,Numberofads NVARCHAR,Totaladsinthatlocation NVARCHAR,typeofads NVARCHAR,Visibilitycode NVARCHAR,Visibilitydescription NVARCHAR,Verificationdate NVARCHAR,Verificationdescription NVARCHAR,Versiocode NVARCHAR,Versiondescription NVARCHAR,Productcode NVARCHAR,Constant NVARCHAR,CategoryCode NVARCHAR,Categorydescription NVARCHAR,Subsectorcode NVARCHAR,Subsectordescription NVARCHAR,Sectorcode NVARCHAR,Sectordescription NVARCHAR,Betweenavenue NVARCHAR,Brandcode NVARCHAR,Branddescription NVARCHAR,Announcercode NVARCHAR,Announcerdescription NVARCHAR,Imagecode NVARCHAR,Agencycode NVARCHAR,Agencyname NVARCHAR,Condition NVARCHAR,Obstruction NVARCHAR); ");
						Toast.makeText(getBaseContext(),
								"Records are deleted successfully",
								Toast.LENGTH_SHORT).show();
						break;

					case DialogInterface.BUTTON_NEGATIVE:
						// No button clicked
						break;
					}
				}
			};

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Are you sure you want to delete?")
					.setPositiveButton("Yes", dialogClickListener)
					.setNegativeButton("No", dialogClickListener).show();
			break;

		default:
			break;
		}

	}

}
